/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.Iterator;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.admin.client.subcomponents.ProductXSellSubComponent;
import com.elf.gwt.common.client.access.RequestParameter;
import com.elf.gwt.common.client.model.DetailedProductModel;
import com.elf.gwt.common.client.model.EditProductXsellMetaModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.Label;

/**
 *
 */
public class EditProductXsellComponent extends JSONServerComponent
{	
	protected static final String Update = "UpdateProduct";

	EditProductXsellMetaModel productXsellMetaModel;
	DetailedProductModel productModel;

	protected int selectedGroupId = 0;
	protected int selectedCategoryId = 0;
	protected int selectedProductId = 0;

	protected String action = null;

	protected ProductXSellSubComponent productXSellSubComponent;

	/**
	 * Constructor
	 */
	public EditProductXsellComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		setupTimer();
	}

	public void additionalRequestParameters()
	{
		addRequestParameter(new RequestParameter ("productId", selectedProductId));
		addRequestParameter(new RequestParameter ("categoryId", selectedCategoryId));
		addRequestParameter(new RequestParameter("action", action));

		if (action != null && action.equals(Update))
		{
			if (productXSellSubComponent != null)
			{
				Iterator<RequestParameter> rpIter = productXSellSubComponent.getAdditionalRequestParameters().iterator();
				while (rpIter.hasNext()) {
					RequestParameter rp = rpIter.next();
					addRequestParameter(rp);
				}
			}
		}
		action = "";
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		productXsellMetaModel = new EditProductXsellMetaModel(jsonObject);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		productModel = new DetailedProductModel(jsonObject, productXsellMetaModel.getImageWidthMax(), productXsellMetaModel.getImageHeightMax(), true);
	}

	public void buildFromModel() throws Exception
	{
		clear();
		productXSellSubComponent = null;

		//Buttons
		HorizontalPanel buttonPanel = new HorizontalPanel();
		buttonPanel.setStyleName("ZButtons");

		Button backButton = new Button("back", 
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						String history = "CategoriesProductsComponent" + "--" + selectedGroupId + "--" + selectedCategoryId + "--" + 0 + "--" + productModel.getId();
						onSelection(history);
					}}
		);
		backButton.addStyleName("ComponentLink");
		buttonPanel.add(backButton);
		buttonPanel.add(new InlineHTML("&nbsp;&nbsp;"));

		if (productModel.getId() > 0) {
			Button saveButton = new Button("save",
					new ClickHandler() {
						public void onClick(ClickEvent event) {
							submit(Update);
						}}
			);
			saveButton.addStyleName("ComponentLink");
			buttonPanel.add(saveButton);
			buttonPanel.add(new InlineHTML("&nbsp;&nbsp;"));
		}

		add(buttonPanel);
		add(new InlineHTML("<br/><br/><br/>"));

		add (new Label(productModel.getName()));
		add (new Label(productModel.getModel()));
		
		productXSellSubComponent = new ProductXSellSubComponent(productXsellMetaModel, adminController, handle, null);
		productXSellSubComponent.build();
		add(productXSellSubComponent);

		onBuilt();
	}

	public boolean updateFromHistoryParameters(String[] params)
	{
		selectedProductId = 0;
		try {
			selectedGroupId = Integer.parseInt(params[1]);
			CatalogGroupComponent catalogGroupComponent = (CatalogGroupComponent) controller.getComponent("CatalogGroupComponent");
			catalogGroupComponent.setSelectedGroupModel(selectedGroupId);	
			catalogGroupComponent.setSelectedInGUI();
			adminController.getNavigationStackPanel().showStack(catalogGroupComponent.getIndex());	

			selectedCategoryId = Integer.parseInt(params[2]);

			selectedProductId = Integer.parseInt(params[3]);
		}
		catch (Exception e) {}

		return super.updateFromHistoryParameters(params);
	}

	protected void submit(String action)
	{
		this.action = action;
		Window.scrollTo(0, 0);
		build(true);
	}
}
